#include "CustomObject.h"
#include "ShootEmUp.h"
#include "GameManager.h"

using namespace Ogre;
using namespace std;

int CustomObject::ID=0;
const char* CustomObject::DEFAULT_NAME="custom_";
const char* CustomObject::DEFAULT_NODE_NAME="CustomObjectNode";

CustomObject::CustomObject(Ogre::Entity * entity)
:Item(CustomObject::DEFAULT_NAME + StringConverter::toString(CustomObject::ID))
{
	CustomObject::ID++;
	mSceneMgr = ShootEmUp::getSingleton().getSceneManager();
	mEntity = entity;
	initialize();
}

CustomObject::~CustomObject()
{}

void CustomObject::initialize()
{
	moveSpeed = ShootEmUp::getSingleton().getScrollingSpeed();
	mEntity->setUserObject( this );
	mSceneNode = mSceneMgr->getSceneNode(CustomObject::DEFAULT_NODE_NAME)->createChildSceneNode(CustomObject::DEFAULT_NODE_NAME+string("_")+getName());
	Real side(Ogre::Math::SymmetricRandom());
	Vector3 initialPosition;
	if( side > 0)
		initialPosition = Vector3( 
			/*Math::RangeRandom(ShootEmUp::WORLD_MIN.x, ShootEmUp::WORLD_MAX.x),*/
			ShootEmUp::WORLD_MAX.x + 100,
			ShootEmUp::WORLD_MIN.y,
			ShootEmUp::WORLD_MAX.z
		);
	else
		initialPosition = Vector3( 
			/*Math::RangeRandom(ShootEmUp::WORLD_MIN.x, ShootEmUp::WORLD_MAX.x),*/
			ShootEmUp::WORLD_MIN.x - 100,
			ShootEmUp::WORLD_MIN.y,
			ShootEmUp::WORLD_MAX.z
		);
	mSceneNode->setPosition( initialPosition );
	if ( mEntity->isAttached() )
		mEntity->getParentSceneNode()->detachObject( mEntity );
	mSceneNode->attachObject( mEntity );
}


void CustomObject::collision(Item* _item)
{}

void CustomObject::collisionWithBullet(Bullet* _bullet)
{}

void CustomObject::collisionWithEnemy(Enemy* _enemy)
{}

void CustomObject::collisionWithMainCharacter(MainCharacter* _mainCharacter)
{}

void CustomObject::collisionWithBonus(Bonus* _bonus)
{}

void CustomObject::collisionWithWeapon(Weapon* weapon)
{}

bool CustomObject::frameStarted(const FrameEvent& evt)
{
	move();
	return true;
}

void CustomObject::collisionWithFish(Fish* _fish)
{
}

bool CustomObject::move(void)
{
	if ( mSceneNode->getPosition().z >= (ShootEmUp::WORLD_MIN.z - 100) )
		mSceneNode->translate(Vector3::NEGATIVE_UNIT_Z * moveSpeed);
	else
		GameManager::getSingleton().destroyCustomObject( this );
	return true;
}

void CustomObject::reset()
{
	Real side(Ogre::Math::SymmetricRandom());
	Vector3 initialPosition;
	if( side > 0)
		initialPosition = Vector3( 
			/*Math::RangeRandom(ShootEmUp::WORLD_MIN.x, ShootEmUp::WORLD_MAX.x),*/
			ShootEmUp::WORLD_MAX.x + 100,
			ShootEmUp::WORLD_MIN.y,
			ShootEmUp::WORLD_MAX.z
		);
	else
		initialPosition = Vector3( 
			/*Math::RangeRandom(ShootEmUp::WORLD_MIN.x, ShootEmUp::WORLD_MAX.x),*/
			ShootEmUp::WORLD_MIN.x - 100,
			ShootEmUp::WORLD_MIN.y,
			ShootEmUp::WORLD_MAX.z
		);
	mSceneNode->setPosition( initialPosition );
	if ( mEntity->isAttached() )
		mEntity->getParentSceneNode()->detachObject( mEntity );
	mSceneNode->attachObject( mEntity );
}